package com.lbs.jiepiaisi.activity;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.base.BaseActivityManager;
import com.lbs.jiepiaisi.base.BaseFragmentActivity;
import com.lbs.jiepiaisi.fragment.AlarmListFragment;
import com.lbs.jiepiaisi.fragment.EquipListFragment;
import com.lbs.jiepiaisi.fragment.HomeFragment;
import com.lbs.jiepiaisi.fragment.MineFragment;
import com.lbs.jiepiaisi.view.AccountViewPager;

public class PlatformMainActivity extends BaseFragmentActivity {

    private Fragment[] fragments = {new HomeFragment(),new EquipListFragment(),new AlarmListFragment(),new MineFragment()};
    private String[] titles = {"首页","列表","告警","我的"};
    private int[] icons_normal = {R.mipmap.home_normal,R.mipmap.list_normal,R.mipmap.alarm_normal,R.mipmap.me_normal};
    private int[] icons_select = {R.mipmap.home_select,R.mipmap.list_select,R.mipmap.alarm_select,R.mipmap.me_select};

    private TabLayout tabLayout;
    private AccountViewPager viewPager;

    @Override
    protected void getExtras() {
        setContentView(R.layout.activity_platform_main_fragment);
    }

    @Override
    protected void findView() {

        tabLayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.viewpager);

        viewPager.setOffscreenPageLimit(4);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments[i];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });


        tabLayout.setupWithViewPager(viewPager);
        //tabLayout.getTabAt(0).select();

//        for (int i = 0; i < titles.length; i++) {
//            if (i == 0) {// 默认选中第一个
//                tabLayout.getTabAt(0).setIcon(icons_select[0]);
//            } else {
//                tabLayout.getTabAt(i).setIcon(icons_normal[i]);
//            }
//        }

        //初始化菜单栏显示
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.account_tab_item, null);
            TextView mTabText = view.findViewById(R.id.tab_item_text);
            ImageView mTabIcon = view.findViewById(R.id.tab_item_img);
            mTabText.setText(titles[i]);
            mTabIcon.setImageResource(icons_normal[i]);
            if (i == 0) {
                mTabIcon.setImageResource(icons_select[i]);
                mTabText.setTextColor(ContextCompat.getColor(this, R.color.blue));
            }
            tabLayout.getTabAt(i).setCustomView(view);
        }


        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                itemSelect(tab);
                viewPager.setCurrentItem(tab.getPosition());
//                for (int i = 0; i < titles.length; i++) {
//                    if (tab == tabLayout.getTabAt(i)) {
//                        tabLayout.getTabAt(i).setIcon(icons_select[i]);
//                        viewPager.setCurrentItem(i);
//                    }
//                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                itemDeselect(tab);
//                for (int i = 0; i < titles.length; i++) {
//                    if (tab == tabLayout.getTabAt(i)) {
//                        tabLayout.getTabAt(i).setIcon(icons_normal[i]);
//                    }
//                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    //选中，改变其样式
    private void itemSelect(TabLayout.Tab tab) {
        View customView = tab.getCustomView();
        TextView tabText = customView.findViewById(R.id.tab_item_text);
        ImageView tabIcon = customView.findViewById(R.id.tab_item_img);
        tabText.setTextColor(ContextCompat.getColor(this, R.color.base_color));
        String stitle = tabText.getText().toString();
        for(int i = 0; i < titles.length; i++){
            if(titles[i].equals(stitle)){
                //Toast.makeText(MainActivity.this,"xxx+"+i,Toast.LENGTH_SHORT).show();
                tabIcon.setImageResource(icons_select[i]);
            }
        }
    }
    //非选中，改变其样式
    private void itemDeselect(TabLayout.Tab tab) {
        View customView = tab.getCustomView();
        TextView tabText = customView.findViewById(R.id.tab_item_text);
        ImageView tabIcon = customView.findViewById(R.id.tab_item_img);
        tabText.setTextColor(ContextCompat.getColor(this, R.color.gray_8f));
        String stitle = tabText.getText().toString();
        for(int i = 0; i < titles.length; i++){
            if(titles[i].equals(stitle)){
                tabIcon.setImageResource(icons_normal[i]);
            }
        }
    }

    @Override
    protected void setListener() {

    }

    /**
     * 退出系统
     */
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                BaseActivityManager.getInstance().finishAll();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//        //添加layout大小发生改变监听器
//        activityRootView.addOnLayoutChangeListener(this);
//    }
//
//    @Override
//    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//        //old是改变前的左上右下坐标点值，没有old的是改变后的左上右下坐标点值
//
////      System.out.println(oldLeft + " " + oldTop +" " + oldRight + " " + oldBottom);
////      System.out.println(left + " " + top +" " + right + " " + bottom);
//
//
//        //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高，就认为软键盘弹起
//        if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){
//
//            //Toast.makeText(OpenFragmentActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show();
//            mLinBottom.setVisibility(View.GONE);
////            activityRootView.scrollTo(0,keyHeight + 20);
////            findViewById(R.id.fragment_container).scrollTo(0,-250);
//        }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){
//
//            //Toast.makeText(OpenFragmentActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show();
//            mLinBottom.setVisibility(View.VISIBLE);
////            activityRootView.scrollTo(0,0);
//            //activityRootView.scrollTo(0,0);
//            //activityRootView.scrollTo(0,keyHeight/2);
////            findViewById(R.id.fragment_container).scrollTo(0,0);
//        }
//
////        if(hasFocus){
////            mLinBottom.setVisibility(View.GONE);
////        }else {
////            mLinBottom.setVisibility(View.VISIBLE);
////        }
//    }
}
